package de.slothsoft.wasbinich.swingclient.io;

import java.awt.Component;
import java.io.File;
import java.util.prefs.Preferences;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public final class Dialogs {

    private static final String PREF_IMAGE_CHOOSER = "PREF_IMAGE_CHOOSER";
    private static final String SUFFIX_PATH = "_PATH";
    private static final String SUFFIX_FILE_FILTER = "_FILE_FILTER";

    private static JFileChooser imageChooser;

    /*
     * File chooser for images
     */

    public static File[] selectImageFiles(Component parent) {
	JFileChooser chooser = createImageChooser();
	chooser.setMultiSelectionEnabled(true);
	if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
	    storePreferences(chooser, PREF_IMAGE_CHOOSER);
	    return chooser.getSelectedFiles();
	}
	return new File[0];
    }

    public static File selectImageFile(Component parent) {
	JFileChooser chooser = createImageChooser();
	if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
	    storePreferences(chooser, PREF_IMAGE_CHOOSER);
	    return chooser.getSelectedFile();
	}
	return null;
    }

    private synchronized static JFileChooser createImageChooser() {
	if (imageChooser == null) {
	    imageChooser = new JFileChooser();
	    imageChooser.setAcceptAllFileFilterUsed(true);
	    FileNameExtensionFilter imageFileFilter = new FileNameExtensionFilter("Image Files", "jpg", "jpeg", "gif",
		    "png");
	    imageChooser.addChoosableFileFilter(imageFileFilter);
	    imageChooser.setFileFilter(imageFileFilter);
	    reloadPreferences(imageChooser, PREF_IMAGE_CHOOSER);
	}
	return imageChooser;
    }

    private static void reloadPreferences(JFileChooser chooser, String preference) {
	String path = getPreferences().get(preference + SUFFIX_PATH, null);
	chooser.setCurrentDirectory(path == null ? null : new File(path));
	int filterIndex = getPreferences().getInt(preference + SUFFIX_FILE_FILTER, -1);
	if (filterIndex != -1) {
	    if (filterIndex >= chooser.getChoosableFileFilters().length) {
		filterIndex = 0;
	    }
	    chooser.setFileFilter(chooser.getChoosableFileFilters()[filterIndex]);
	}
    }

    private static void storePreferences(JFileChooser chooser, String preference) {
	getPreferences().put(preference + SUFFIX_PATH, chooser.getCurrentDirectory().toString());
	getPreferences().putInt(preference + SUFFIX_FILE_FILTER, getFilterIndex(chooser));
    }

    private static int getFilterIndex(JFileChooser chooser) {
	FileFilter[] filters = chooser.getChoosableFileFilters();
	FileFilter selectedFilter = chooser.getFileFilter();
	if (selectedFilter == null)
	    return -1;
	for (int i = 0; i < filters.length; i++) {
	    if (selectedFilter.equals(filters[i]))
		return i;
	}
	return -1;
    }

    private static Preferences getPreferences() {
	return Preferences.userRoot().node(Dialogs.class.getName());
    }
}
